<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-valet" tabindex="-1"><a class="header-anchor" href="#laravel-valet"><span>Laravel Valet</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#valet-or-homestead">Valet 还是 Homestead</a></li>
</ul>
</li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#upgrading">升级</a></li>
</ul>
</li>
<li><a href="#serving-sites">服务站点</a>
<ul>
<li><a href="#the-park-command">Park 命令</a></li>
<li><a href="#the-link-command">Link 命令</a></li>
<li><a href="#securing-sites">使用 TLS 构建安全站点</a></li>
</ul>
</li>
<li><a href="#sharing-sites">共享站点</a></li>
<li><a href="#site-specific-environment-variables">网站特定环境变量</a></li>
<li><a href="#custom-valet-drivers">自定义 Valet 驱动</a>
<ul>
<li><a href="#local-drivers">本地驱动</a></li>
</ul>
</li>
<li><a href="#other-valet-commands">其他 Valet 命令</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Valet 是 Mac 极简主义者的 Laravel 开发环境。没有 Vagrant，不需要配置 <code v-pre>/etc/hosts</code> 文件。甚至可以使用本地隧道公开共享你的站点。<em>是的，我们都喜欢Valet。</em></p>
<p>Laravel Valet 为您的 Mac 设置了开机后始终在后台运行 <a href="https://www.nginx.com/" target="_blank" rel="noopener noreferrer">Nginx</a> 服务。然后，Valet 使用 <a href="https://en.wikipedia.org/wiki/Dnsmasq" target="_blank" rel="noopener noreferrer">DnsMasq</a> 将所有指向安装在本地的计算机站点请求代理到 <code v-pre>*.test</code> 结尾的域名上。</p>
<p>换句话说，一个速度极快的 Laravel 开发环境只占用 7MB 内存。Valet 并不是想要完全替换 Vagrant 或 Homestead，只是提供另外一种使用起来更加灵活、方便、以及内存占用更小的选择。</p>
<p>Valet 支持但不局限于以下内容：</p>
<ul>
<li><a href="https://laravel.com/" target="_blank" rel="noopener noreferrer">Laravel</a></li>
<li><a href="https://lumen.laravel.com/" target="_blank" rel="noopener noreferrer">Lumen</a></li>
<li><a href="https://roots.io/bedrock/" target="_blank" rel="noopener noreferrer">Bedrock</a></li>
<li><a href="https://cakephp.org/" target="_blank" rel="noopener noreferrer">CakePHP 3</a></li>
<li><a href="https://www.concrete5.org/" target="_blank" rel="noopener noreferrer">Concrete5</a></li>
<li><a href="https://contao.org/en/" target="_blank" rel="noopener noreferrer">Contao</a></li>
<li><a href="https://craftcms.com/" target="_blank" rel="noopener noreferrer">Craft</a></li>
<li><a href="https://www.drupal.org/" target="_blank" rel="noopener noreferrer">Drupal</a></li>
<li><a href="https://jigsaw.tighten.co/" target="_blank" rel="noopener noreferrer">Jigsaw</a></li>
<li><a href="https://www.joomla.org/" target="_blank" rel="noopener noreferrer">Joomla</a></li>
<li><a href="https://github.com/themsaid/katana" target="_blank" rel="noopener noreferrer">Katana</a></li>
<li><a href="https://getkirby.com/" target="_blank" rel="noopener noreferrer">Kirby</a></li>
<li><a href="https://magento.com/" target="_blank" rel="noopener noreferrer">Magento</a></li>
<li><a href="https://octobercms.com/" target="_blank" rel="noopener noreferrer">OctoberCMS</a></li>
<li><a href="https://sculpin.io/" target="_blank" rel="noopener noreferrer">Sculpin</a></li>
<li><a href="https://www.slimframework.com/" target="_blank" rel="noopener noreferrer">Slim</a></li>
<li><a href="https://statamic.com/" target="_blank" rel="noopener noreferrer">Statamic</a></li>
<li>Static HTML</li>
<li><a href="https://symfony.com/" target="_blank" rel="noopener noreferrer">Symfony</a></li>
<li><a href="https://wordpress.org/" target="_blank" rel="noopener noreferrer">WordPress</a></li>
<li><a href="https://framework.zend.com/" target="_blank" rel="noopener noreferrer">Zend</a></li>
</ul>
<p>当然，您也可以使用 <a href="#custom-valet-drivers">custom drivers</a> 来拓展您的 Valet。</p>
<h3 id="valet-还是-homestead" tabindex="-1"><a class="header-anchor" href="#valet-还是-homestead"><span>Valet 还是 Homestead</span></a></h3>
<p>您应该知道，Laravel 还提供了另外一种本地开发环境 <a href="https://learnku.com/docs/laravel/6.0/homestead" target="_blank" rel="noopener noreferrer">Homestead</a>。Homestead 和 Valet 的不同之处在于目标受众和本地开发的方式。Homestead 提供了一个完整的、具有自动化的 Nginx 配置的 Ubuntu 虚拟机。如果你想在 Windows／Linux 上实现完全虚拟化的 Linux 开发环境，Homestead 是一个不错的选择。</p>
<p>Valet 仅支持 Mac，并要求你将 PHP 和数据库服务器直接安装到本地机器上。这可以很容易地通过使用 <a href="https://brew.sh/" target="_blank" rel="noopener noreferrer">Homebrew</a> 命令来实现，比如 <code v-pre>brew install php</code> 和 <code v-pre>brew install mysql</code>。Valet 提供了一个极快的、资源消耗最少本地开发环境，非常适合只需要 PHP／MySQL 并且不需要虚拟开发环境的开发人员。</p>
<p>Valet 和 Homestead 都是配置 Laravel 开发环境的绝佳选择。选择哪一个仅仅取决于个人喜好和团队的需求。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p><strong>Valet 需要 macOS 系统和 <a href="https://brew.sh/" target="_blank" rel="noopener noreferrer">Homebrew</a>。在安装之前，要确保没有其它程序（如 Apache 或 Nginx）占用了本地机器的 80 端口。</strong></p>
<ul>
<li>使用 <code v-pre>brew update</code> 将 <a href="https://brew.sh/" target="_blank" rel="noopener noreferrer">Homebrew</a> 安装或更新到最新版本。</li>
<li>使用 Homebrew 的 <code v-pre>brew install php</code> 命令安装 PHP 7.3。</li>
<li>安装 <a href="https://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a>.</li>
<li>使用 Composer 的 <code v-pre>composer global require laravel/valet</code> 命令安装 Valet。并确保 <code v-pre>~/.composer/vendor/bin</code> 目录在系统的 &quot;PATH&quot; 中。</li>
<li>运行 <code v-pre>valet install</code> 命令来配置和安装 Valet 和 DnsMasq，并注册 Valet 后台服务，随系统运行自行启动。</li>
</ul>
<p>安装完 Valet 后，可以尝试使用类似 <code v-pre>ping foobar.test</code> 的命令在终端上 ping 任何一个 <code v-pre>*.test</code> 的域名。如果 Valet 安装正确，可以在终端上看到来自 <code v-pre>127.0.0.1</code> 的响应。</p>
<p>每次机器启动时，Valet 会自动启动其进程。所以只要完成了 Valet 的初始化，就无需再次运行 <code v-pre>valet start</code> 或 <code v-pre>valet install</code>。</p>
<h4 id="使用其它域名" tabindex="-1"><a class="header-anchor" href="#使用其它域名"><span>使用其它域名</span></a></h4>
<p>默认情况下，Valet 使用 <code v-pre>.test</code> 顶级域名为你的项目提供服务。如果你想使用其它域名，可以使用 <code v-pre>valet tld tld-name</code> 命令。</p>
<p>例如，如果你想使用 <code v-pre>.app</code> 来代替 <code v-pre>.test</code>，运行 <code v-pre>valet tld app</code>，Valet 会自动将站点顶级域名改为 <code v-pre>*.app</code> 。</p>
<h4 id="数据库" tabindex="-1"><a class="header-anchor" href="#数据库"><span>数据库</span></a></h4>
<p>如果你要使用数据库，请在终端运行 <code v-pre>brew install mysql@5.7</code> 安装 MySQL。一旦安装完成，你可以使用 <code v-pre>brew services start mysql@5.7</code> 命令启动 MySQL。然后，你可以使用 <code v-pre>root</code> 用户名和空字符串密码连接到 <code v-pre>127.0.0.1</code> 的数据库。</p>
<h4 id="php-版本" tabindex="-1"><a class="header-anchor" href="#php-版本"><span>PHP 版本</span></a></h4>
<p>Valet 允许你使用 <code v-pre>valet use php@version</code> 命令来切换 PHP 版本。如果指定版本尚未安装，Valet 将通过 Brew 来安装指定的 PHP 版本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">valet <span class="token keyword">use</span> <span class="token package">php</span>@<span class="token number">7.2</span></span>
<span class="line"></span>
<span class="line">valet <span class="token keyword">use</span> <span class="token package">php</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="升级" tabindex="-1"><a class="header-anchor" href="#升级"><span>升级</span></a></h3>
<p>你可以在终端使用 <code v-pre>composer global update</code> 命令来更新 Valet。升级后，如有需要，最好再次运行 <code v-pre>valet install</code> ，以便 Valet 对配置文件进行升级。</p>
<h4 id="升级到-valet-2-0" tabindex="-1"><a class="header-anchor" href="#升级到-valet-2-0"><span>升级到 Valet 2.0</span></a></h4>
<p>Valet 2.0 将 Valet 底层的 Web 服务器从 Caddy 转移到 Nginx。升级到此版本之前，你应该运行以下命令停止并卸载现有的 Caddy 进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">valet stop</span>
<span class="line">valet uninstall</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，就根据你采用的安装方式来升级 Valet （通常是通过 Git 或 Composer ）。如果是通过 Composer 安装了 Valet ，则应使用以下命令更新到最新的主要版本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">global</span> <span class="token keyword">require</span> laravel<span class="token operator">/</span>valet</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果更新了 Valet 的源码，你应该执行 <code v-pre>install</code> 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">valet install</span>
<span class="line">valet restart</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>升级过后，可能需要重新设置或重新链接你的站点。</p>
<h2 id="服务站点" tabindex="-1"><a class="header-anchor" href="#服务站点"><span>服务站点</span></a></h2>
<p>安装了 Valet 之后，你就可以开始设置站点。Valet 提供两个命令来为 Laravel 的站点提供服务：<code v-pre>park</code> 和 <code v-pre>link</code>。</p>
<p><strong><code v-pre>park</code> 命令</strong></p>
<ul>
<li>运行 <code v-pre>mkdir ~/Sites</code> 命令在 Mac 上创建一个新的目录。接下来，运行 <code v-pre>cd ~/Sites</code> 和 <code v-pre>valet park</code>将当前的工作目录作为 Valet 搜索站点的路径。</li>
<li>接下来，在这个目录中创建一个新的 Laravel 站点：<code v-pre>laravel new blog</code>。</li>
<li>在浏览器中打开 <code v-pre>http://blog.test</code>。</li>
</ul>
<p><strong>就这么多。</strong> 现在，你在 『parked』的目录中创建的任何 Laravel 项目都将自动使用 <code v-pre>http://folder-name.test</code> 这种方式访问。</p>
<p><strong><code v-pre>link</code> 命令</strong></p>
<p>如果要在目录中提供单个站点而不是整个目录，就使用 <code v-pre>link</code> 命令。</p>
<ul>
<li>要使用该命令，先在终端里切换到你的某个项目并运行 <code v-pre>valet link app-name</code>。Valet 会在 <code v-pre>~/.config/valet/Sites</code> 中创建一个符号链接指向当前的目录。</li>
<li>运行 <code v-pre>link</code> 命令后，你可以在浏览器通过 <code v-pre>http://app-name.test</code> 访问站点。</li>
</ul>
<p>运行 <code v-pre>valet links</code> 命令可以查看所有目录链接的列表。你还可以使用 <code v-pre>valet unlink app-name</code> 来删除符号链接。</p>
<blockquote>
<p>Tip：你可以使用 <code v-pre>valet link</code> 将多个（子）域名指向同一个应用。要添加子域名或其它域名到应用，可以在应用目录下运行 <code v-pre>valet link subdomain.app-name</code>。</p>
</blockquote>
<p><strong>使用 TLS 保护站点</strong></p>
<p>默认情况下，Valet 服务站点通过纯 HTTP 的方式。但是，如果你想一个站点使用 HTTP/2 通过加密 TLS 提供服务，使用 <code v-pre>secure</code> 命令。例如，如果你的站点通过 Valet 在 <code v-pre>laravel.test</code> 域上提供服务，你应当运行如下的命令去保护它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    valet secure laravel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要一个站点『解除保护』并恢复为通过纯 HTTP 提供服务它的流量，使用 <code v-pre>unsecure</code> 命令。与 <code v-pre>secure</code> 命令一样，该命令接受你希望去解除保护的主机名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    valet unsecure laravel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="共享站点" tabindex="-1"><a class="header-anchor" href="#共享站点"><span>共享站点</span></a></h2>
<p>Valet 甚至包括与世界共享你的本地站点的命令。一旦安装 Valet 后无需安装其它软件。</p>
<p>要共享站点，在终端中导航你的站点目录并运行 <code v-pre>valet share</code> 命令。一个公开可访问的 URL 将被插入到剪贴板中，并可以直接粘贴到你的浏览器中。仅此而已。</p>
<p>要停止共享你的站点，请按 <code v-pre>Control + C</code> 去取消该过程。</p>
<h2 id="网站特定环境变量" tabindex="-1"><a class="header-anchor" href="#网站特定环境变量"><span>网站特定环境变量</span></a></h2>
<p>有些使用其他框架的应用程序可能需要依赖一些服务器环境变量但是没有在你的项目中提供配置。 Valet可以通过添加<code v-pre>.valet-env.php</code>文件来允许你针对单独的网站进行设置环境变量。这些环境变量将会添加到 <code v-pre>$_SERVER</code> 的全局数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'WEBSITE_NAME'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'My Blog'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义-valet-驱动" tabindex="-1"><a class="header-anchor" href="#自定义-valet-驱动"><span>自定义 Valet 驱动</span></a></h2>
<p>你可以编写你自己的 Valet『驱动』来为 Valet 原本不支持的其它框架或 CMS 上运行的 PHP 应用程序提供服务。当你安装 Valet 时，会创建一个包含 <code v-pre>SampleValetDriver.php</code> 文件的 <code v-pre>~/.config/valet/Drivers</code> 目录。该文件包含一个示例驱动程序实现，演示了如何编写一个自定义的驱动程序。编写驱动程序仅需要你去实现三个方法：<code v-pre>serves</code>，<code v-pre>isStaticFile</code> 和 <code v-pre>frontControllerPath</code>。</p>
<p>这三个方法都接受 <code v-pre>$sitePath</code>，<code v-pre>$siteName</code> 和 <code v-pre>$uri</code> 值作为参数。<code v-pre>$sitePath</code> 是你的机器上提供站点的完全限定路径。比如 <code v-pre>/Users/Lisa/Sites/my-project</code>。<code v-pre>$siteName</code> 是域（<code v-pre>my-project</code>）的『主机』/『站点名称』部分。<code v-pre>$uri</code> 是即将到来的请求 URL（/foo/bar）。</p>
<p>一旦你完成你的自定义 Valet 驱动，使用 <code v-pre>FrameworkValetDriver.php</code> 命名约定将它放置在 <code v-pre>~/.config/valet/Drivers</code> 目录中。例如，如果你为 WordPress 编写了一个自定义的 valet 驱动，你的文件名称应该是 <code v-pre>WordPressValetDriver.php</code>。</p>
<p>我们来看看自定义的 Valet 驱动程序应该实现的每种方法的示例实现。</p>
<h4 id="serves-方法" tabindex="-1"><a class="header-anchor" href="#serves-方法"><span><code v-pre>serves</code> 方法</span></a></h4>
<p>如果你的驱动程序应当处理即将到来的请求时， <code v-pre>serves</code> 方法应该返回 <code v-pre>true</code>。否则，此方法应当返回 <code v-pre>false</code>。因此，在此方法中，你应该你企图确定给定的 <code v-pre>$sitePath</code> 是否包含你尝试提供的类型的项目。</p>
<p>例如，让我们假装正在编写一个 <code v-pre>WordPressValetDriver</code>。我们的 <code v-pre>serves</code> 方法可能看起来如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 确定驱动程序是否满足请求。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$sitePath</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$siteName</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$uri</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">serves</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token punctuation">,</span> <span class="token variable">$siteName</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">is_dir</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token operator">.</span><span class="token string single-quoted-string">'/wp-admin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isstaticfile-方法" tabindex="-1"><a class="header-anchor" href="#isstaticfile-方法"><span><code v-pre>isStaticFile</code> 方法</span></a></h4>
<p><code v-pre>isStaticFile</code> 应当确定即将到来的请求是否针对一个『静态』文件，比如：图片和样式表。如果文件是静态的，此方法应当返回静态文件在磁盘上的完全限定路径。如果即将到来的请求不是针对一个静态文件，这个方法应当返回 <code v-pre>false</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     *  确定即将到来的请求是否针对静态文件。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$sitePath</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$siteName</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$uri</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span><span class="token punctuation">|</span><span class="token keyword">false</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">isStaticFile</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token punctuation">,</span> <span class="token variable">$siteName</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">file_exists</span><span class="token punctuation">(</span><span class="token variable">$staticFilePath</span> <span class="token operator">=</span> <span class="token variable">$sitePath</span><span class="token operator">.</span><span class="token string single-quoted-string">'/public/'</span><span class="token operator">.</span><span class="token variable">$uri</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$staticFilePath</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：如果 <code v-pre>serves</code> 方法对即将到来的请求且请求 URI 不是 <code v-pre>/</code> 返回 <code v-pre>true</code> 时，才会调用 <code v-pre>isStaticFile</code> 方法。</p>
</blockquote>
<h4 id="frontcontrollerpath-方法" tabindex="-1"><a class="header-anchor" href="#frontcontrollerpath-方法"><span><code v-pre>frontControllerPath</code> 方法</span></a></h4>
<p><code v-pre>frontControllerPath</code> 方法应当返回你的应用程序的『前端控制器』完全限定的路径，它通常是你的 『index.php』文件或者等效的文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取对应用程序的前端控制器的完全解析路径。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$sitePath</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$siteName</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$uri</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">frontControllerPath</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token punctuation">,</span> <span class="token variable">$siteName</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$sitePath</span><span class="token operator">.</span><span class="token string single-quoted-string">'/public/index.php'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="本地驱动" tabindex="-1"><a class="header-anchor" href="#本地驱动"><span>本地驱动</span></a></h3>
<p>如果你想为单个应用程序自定义 Valet 驱动程序，在应用程序的根目录下创建一个 <code v-pre>LocalValetDriver.php</code> 文件。你的自定义驱动程序可以扩展 <code v-pre>ValetDriver</code> 基类或者继承一个现有的应用程序的特定驱动程序，比如：<code v-pre>LaravelValetDriver</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">class</span> <span class="token class-name-definition class-name">LocalValetDriver</span> <span class="token keyword">extends</span> <span class="token class-name">LaravelValetDriver</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token doc-comment comment">/**</span>
<span class="line">         * 确定驱动程序是否满足请求。</span>
<span class="line">         *</span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$sitePath</span></span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$siteName</span></span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$uri</span></span>
<span class="line">         * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line">         */</span></span>
<span class="line">        <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">serves</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token punctuation">,</span> <span class="token variable">$siteName</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token doc-comment comment">/**</span>
<span class="line">         * 获取对应用程序的前端控制器的完全解析路径。</span>
<span class="line">         *</span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$sitePath</span></span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$siteName</span></span>
<span class="line">         * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$uri</span></span>
<span class="line">         * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">         */</span></span>
<span class="line">        <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">frontControllerPath</span><span class="token punctuation">(</span><span class="token variable">$sitePath</span><span class="token punctuation">,</span> <span class="token variable">$siteName</span><span class="token punctuation">,</span> <span class="token variable">$uri</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$sitePath</span><span class="token operator">.</span><span class="token string single-quoted-string">'/public_html/index.php'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="其它-valet-命令" tabindex="-1"><a class="header-anchor" href="#其它-valet-命令"><span>其它 Valet 命令</span></a></h2>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>valet forget</code></td>
<td>从一个『驻留』目录运行此命令，从驻留目录列表将其它移除</td>
</tr>
<tr>
<td><code v-pre>valet log</code></td>
<td>从Valet 的服务中查看日志</td>
</tr>
<tr>
<td><code v-pre>valet paths</code></td>
<td>查看所有『驻留』路径</td>
</tr>
<tr>
<td><code v-pre>valet restart</code></td>
<td>重启 Valet 守护进程</td>
</tr>
<tr>
<td><code v-pre>valet start</code></td>
<td>开启 Valet 守护进程</td>
</tr>
<tr>
<td><code v-pre>valet stop</code></td>
<td>停止 Valet 守护进程</td>
</tr>
<tr>
<td><code v-pre>valet trust</code></td>
<td>为 Brew 和 Valet 添加文件修改权限使 Valet 输入命令的时候不需要输入密码</td>
</tr>
<tr>
<td><code v-pre>valet uninstall</code></td>
<td>完成卸载 Valet 守护进程</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/valet/5128" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/val...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/valet/5128" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/val...</a></p>
</blockquote>
</div></template>


